<template>
  <div class="create-service">
    <el-dialog
      title="服务申请"
      :visible.sync="dialogVisible"
      width="800px"
      destroy-on-close
      @close="cancel"
    >
      <DialogDetail :data-list="dataList" />
      <el-form ref="formRef" style="margin-top: 20px;" :model="form" :rules="formRules">
        <el-form-item label="申请系统" prop="requestIds">
          <el-select
            v-model="form.requestIds"
            style="width: 100%;"
            multiple
            clearable
            filterable
            placeholder="请选择申请系统"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.systemName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="申请原因" prop="remarks">
          <el-input v-model="form.remarks" :maxlength="200" placeholder="请填写申请原因" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="interfaceServiceConfirm('formRef')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import DialogDetail from '@/views/myOpenService/components/dialogDetail.vue'
import { addQuerst, getSystemList } from '../../../api/myOpenService'

export default {
  name: 'RequestInterface',
  components: { DialogDetail },
  data() {
    return {
      options: null,
      dataList: {},
      dialogVisible: false,
      form: {
        requestIds: null,
        remarks: null
      },
      formRules: {
        requestIds: { required: true, message: '请选择申请系统', trigger: 'blur' },
        remarks: { required: true, message: '请填写申请原因', trigger: 'blur' }
      }
    }
  },
  created() {
    getSystemList().then(res => {
      if (res.code === '00000') this.options = res.data
    })
  },
  methods: {
    cancel() {
      this.dialogVisible = false
      Object.assign(this.form, {
        requestIds: null,
        remarks: null
      })
    },
    interfaceServiceConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addQuerst({ provideId: this.dataList.id, ...this.form }).then(res => {
            if (res.code === '00000') {
              this.$message.success('申请成功')
              this.$emit('resetBtn')
            }
          }).finally(e => {
            this.dialogVisible = false
          })
        }
      })
    }
  }
}
</script>
